package view;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import resources.ResourceProvider;

import model.Email;
import model.EmailImp;
import model.Template;

/**
 *  Message panel Class to display the Email in the view
 */
public class MessagePanel extends JPanel
{	
	private static final long serialVersionUID = 8416080757033830131L;
	
	private JTextField toOrFromTextField;
	private JTextField subjectTextField;
	private JTextArea messageTextField;
	private JLabel toOrFrom;
    private MailDisplayArea mailDisplayArea;
    private EmailImp email;

	/**
	 * MessagePanel will have mailDisplayArea
	 * @param mailDisplayArea the mail display area reference
	 */
	MessagePanel(MailDisplayArea mailDisplayArea)
	{
	    this.mailDisplayArea = mailDisplayArea;
	    this.initialize();
	}
	
	/**
	 * Constructor
     * @param mailDisplayArea the mail display area reference
	 * @param email the email
	 * @param reply the reply
	 */
	public MessagePanel(MailDisplayArea mailDisplayArea, EmailImp email, boolean reply)
    {
	    this.mailDisplayArea = mailDisplayArea;
	    this.email = email;
        this.initialize();
         
        if(reply || email.getPersistencePath().contains(ResourceProvider.DRAFT))
        {
            this.toOrFrom.setText("To");
            StringBuilder receivers = new StringBuilder("");
            List<String> receiversList = email.getReceiver();
            
            for(int n=0; n < receiversList.size(); n++ )
            {
                receivers.append(receiversList.get(n));
                if(n != (receiversList.size() -1))
                {
                    receivers.append(",");
                }
            }
            
            this.toOrFromTextField.setText(receivers.toString());  
        }
        else
        {
                      
          if(email.getPersistencePath().contains(ResourceProvider.SENT) || email.getPersistencePath().contains(ResourceProvider.OUTBOX))
          {
              this.toOrFrom.setText("To");
              StringBuilder receivers = new StringBuilder("");
              List<String> receiversList = email.getReceiver();
              
              for(int n=0; n < receiversList.size(); n++ )
              {
                  receivers.append(receiversList.get(n));
                  if(n != (receiversList.size() -1))
                  {
                      receivers.append(",");
                  }
              }
              
              this.toOrFromTextField.setText(receivers.toString());
          }
          else
          {
              this.toOrFrom.setText("From");
              this.toOrFromTextField.setText(email.getSender());
          } 
          
          this.toOrFromTextField.setEditable(false);
          this.subjectTextField.setEditable(false);
          this.messageTextField.setEditable(false);
        }
        
        
        this.subjectTextField.setText(email.getSubject());
        this.messageTextField.setText(email.getMessage()); 
    }
	
	/**
	 * Initialization of the MessagePanel
	 */
	private void initialize()
	{
	    this.setBorder(new EmptyBorder(5, 5, 5, 5));
        this.setLayout(null);
        
        toOrFrom = new JLabel("To");
        toOrFrom.setBounds(new Rectangle(32,44,64,30));
        toOrFrom.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(toOrFrom);
        
        JLabel subjectLable = new JLabel("Subject");
        subjectLable.setBounds(new Rectangle(32,104,64,30));
        subjectLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(subjectLable);
        
        JLabel contentLable = new JLabel("Message");
        contentLable.setBounds(new Rectangle(32,164,64,30));
        contentLable.setFont(new Font("Times New Roman",Font.PLAIN,18));
        this.add(contentLable);
        
        toOrFromTextField = new JTextField();
        toOrFromTextField.setBounds(new Rectangle(135,44,400,30));
        this.add(toOrFromTextField);
        
        subjectTextField = new JTextField();
        subjectTextField.setBounds(new Rectangle(135,104,400,30));
        this.add(subjectTextField);
        
        messageTextField = new JTextArea();
        
        JScrollPane scroll = new JScrollPane (messageTextField, 
            JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scroll.setBounds(new Rectangle(135,164,400,320));    
        this.add(scroll);
        
	}

    /**
     * @return return email in the view 
     */
    public Email getEmail()
	{
        String[] receivers = toOrFromTextField.getText().trim().split(",");
        List<String> receiverList = Arrays.asList(receivers);
        String subject = subjectTextField.getText().trim();
        String message = messageTextField.getText().trim();
        
        String dateF = "";
        if((this.email != null) && this.email.getPersistencePath().contains(ResourceProvider.DRAFT))
        {
            dateF = email.getTime();
        }
        else
        {
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            dateF = dateFormat.format(date); 
        }
        
            
        String sender = MessagePanel.this.mailDisplayArea.getEmailApplicationView().
                    getController().getUser();
        
        Email email = new Email.Builder()
        								.setSender(sender)
        								.setReceiver(receiverList)
        								.setSubject(subject)
        								.setMessage(message)
        								.setTimeOfCreation(dateF)
        								.build();
            
        if((this.email != null) && this.email.getPersistencePath().contains(ResourceProvider.DRAFT))
        {
            email.setPersistencePath(this.email.getPersistencePath());
        }
        return email;
	 }
    
    /**
     * load the template to the view for the current email
     * @param tempL the template
     */
    public void loadTemplate(Template tempL)
    {
       List<String> recL = tempL.getTo();
       StringBuilder rec = new StringBuilder();
       for(int n=0;n<recL.size();n++)
       {
           rec.append(recL.get(n));
           if(n != (recL.size()-1))
           {
               rec.append(",");  
           }    
       }
       
       String subject = tempL.getSubject();
       String message = tempL.getMessage();
       toOrFromTextField.setText(rec.toString());
       subjectTextField.setText(subject);
       messageTextField.setText(message);
    }
}
